package com.changgou.goods.controller;

import com.changgou.goods.pojo.Para;
import com.changgou.goods.service.ParaService;
import com.github.pagehelper.PageInfo;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author seafwg
 * @Date 2021/11/30 3:26 下午
 * @Version 1.0
 * @description 参数管理：
 */

@CrossOrigin
@RestController
@RequestMapping(value = "/para")
public class ParaController {

    @Autowired
    private ParaService paraService;

    /**
     * 根据分类ID查询参数列表信息
     * @param cid
     * @return
     */
    @GetMapping(value = "/category/{cid}")
    public Result<List<Para>> findParaByCategoryId(@PathVariable(name = "cid") Integer cid) {
        List<Para> paraList = paraService.findParaByCategory(cid);
        return new Result<>(true, StatusCode.OK, "查询成功", paraList);
    }

    /**
     * 新增参数对象：
     * @param para
     * @return
     */
    @PostMapping(value = "/add")
    public Result add(Para para) {
        int add = paraService.add(para);
        return new Result(true, StatusCode.OK, "新增成功", add);
    }

    /**
     * 根据id删除参数信息：
     * @param id
     * @return
     */
    @DeleteMapping(value = "/delete")
    public Result deleteById(@PathVariable Integer id) {
        int delete = paraService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功", delete);
    }

    /**
     * 更新数据：
     * @param para
     * @param id
     * @return
     */
    @PutMapping(value = "/update/{id}")
    public Result updatePara(@RequestBody Para para, @PathVariable Integer id) {
        paraService.update(para, id);
        return new Result(true, StatusCode.OK, "更新数据成功");
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @PostMapping(value = "/find/{id}")
    public Result<Para> findById(@PathVariable Integer id) {
        Para para = paraService.findById(id);
        return new Result<Para>(true, StatusCode.OK, "查询成功", para);
    }

    /**
     * 查询所有：
     * @return
     */
    @PostMapping(value = "/findAll")
    public Result<List<Para>> findList() {
        List<Para> paraList = paraService.findAll();
        return new Result<List<Para>>(true, StatusCode.OK, "查询成功", paraList);
    }

    /**
     * 多条件查询：
     * @param para
     * @return
     */
    @PostMapping(value = "/search")
    public Result<List<Para>> findAllList(@RequestBody(required = false) Para para) {
        List<Para> paraList = paraService.findList(para);
        return new Result<List<Para>>(true, StatusCode.OK, "查询成功", paraList);
    }

    /**
     * 分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping(value = "/search/{pageSize}/{pageNum}")
    public Result<List<Para>> findPageList(@PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Para> pageInfo = paraService.findPage(pageSize, pageNum);
        return new Result<List<Para>>(true, StatusCode.OK, "查询成功", pageInfo);
    }

    /**
     * 多条件分页查询：
     * @param para
     * @param pageSize
     * @param pageNum
     * @return
     */
    @PostMapping(value = "/search/{pageSize}/{pageNum}")
    public Result<List<Para>> findPageList(@RequestBody(required = false) Para para, @PathVariable Integer pageSize, @PathVariable Integer pageNum) {
        PageInfo<Para> pageInfo = paraService.findPage(para, pageSize, pageNum);
        return new Result<List<Para>>(true, StatusCode.OK, "查询成功", pageInfo);
    }


}
